package permission;

import org.apache.shiro.authz.Permission;

	public class MyPermission implements Permission {
		
		public String getInstanceId() {
			return instanceId;
		}

		public void setInstanceId(String instanceId) {
			this.instanceId = instanceId;
		}

		public String getResourceId() {
			return resourceId;
		}

		public void setResourceId(String resourceId) {
			this.resourceId = resourceId;
		}

		public String getOperrator() {
			return operrator;
		}

		public void setOperrator(String operrator) {
			this.operrator = operrator;
		}
		private String instanceId;
		private String resourceId;
		private String operrator;
		
		

	public MyPermission() {
	}
	
	public MyPermission(String permissionString) {
		String[] strs=permissionString.split("\\+");
		if(strs.length>1) {
			this.setResourceId(strs[0]);
		}
		if (this.getResourceId()==null || "".equals(this.getResourceId())) {
			this.setResourceId("*");
		}
		if(strs.length>2) {
			this.setOperrator(strs[1]);
		}
		if (strs.length>3) {
			this.setInstanceId(strs[2]);
		}
		if (this.getInstanceId()==null||"".equals(this.getInstanceId())) {
			this.setInstanceId("*");
		}
		System.out.println(this);
	}
	@Override
	public String toString() {
		return "MyPermission [instanceId=" + instanceId + ", resourceId=" + resourceId + ", operrator=" + operrator
				+ "]";
	}

	@Override
	public boolean implies(Permission p) {
		// TOIfOifDO Auto-generated method stub
		if (!(p instanceof MyPermission)) {
			return false;
		}
		MyPermission mp=(MyPermission) p;
		if (!(this.getResourceId()).equals("*")||!mp.getResourceId().equals(this.getResourceId())) {
			return false;
		}
		if (!this.getOperrator().equals("*")||!mp.getOperrator().equals(this.getOperrator())) {
			return false;
		}
		if(!this.getInstanceId().equals("*	")||!mp.getInstanceId().equals(this.getInstanceId())) {
			return false;
		}
		return true;
	}
}
